<template>
  <div class="employees-container">
    <div class="app-container">
      <el-card>
        <el-form 
          :model="userInfoForm" 
          :rules="userInfoRules"
          ref="userInfoRef"
          label-width="100px"
          style="width: 50%;margin-top: 20px;">
          <el-form-item label="姓名" prop="username">
            <el-input v-model="userInfoForm.username"></el-input>
          </el-form-item>
          <el-form-item label="手机">
            <el-input v-model="userInfoForm.mobile" disabled></el-input>
          </el-form-item>
          <el-form-item label="入职时间">
            <el-date-picker
              style="width: 100%;"
              v-model="userInfoForm.timeOfEntry"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="员工头像">
            <!-- 1 -->
            
            <!-- <upload-img 
              :url="userInfoForm.staffPhoto"
              @set-img="setImg">
            </upload-img> -->
            <!-- <upload-img 
              :value="userInfoForm.staffPhoto"
              @input="setImg">
            </upload-img> -->
            <upload-img v-model="userInfoForm.staffPhoto"></upload-img>
            <!-- 相当于 -->
            <!-- <upload-img 
              :value="userInfoForm.staffPhoto" 
              @input="userInfoForm.staffPhoto = $event">
            </upload-img> -->
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submit">保存更新</el-button>
            <el-button @click="$router.go(-1)">返回</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailInfo } from '@/api/user'
import UploadImg from '@/components/UploadImg'
import { updateUserInfo } from '@/api/employee'
export default {
  components: {
    UploadImg
  },
  data() {
    return {
      // 表单数据对象
      userInfoForm: {
        username: '', // 员工姓名
        mobile: '', // 手机号
        timeOfEntry: '', // 入职时间
        staffPhoto: '', // 员工头像
      },
      // 表单验证规则对象
      userInfoRules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    // 数据回显
    // 异步 2
    const res = await getUserDetailInfo(this.$route.query.id)
    // console.log(res);
    this.userInfoForm = res.data
  },
  methods: {
    setImg(imgUrl) {
      this.userInfoForm.staffPhoto = imgUrl
    },
    // 保存更新
    submit() {
      // 兜底校验
      this.$refs.userInfoRef.validate(async valid => {
        if (!valid) return
        // 校验通过
        // 发起请求
        await updateUserInfo(this.userInfoForm)
        // 提示用户
        this.$message.success('更新成功')
      })
    }
  }
}
</script>

<style>

</style>